package com.yb.xczx.minio.upload.client;

import com.yb.xczx.minio.upload.bean.MinioProperties;
import com.yb.xczx.upload.bean.UploadRequest;
import com.yb.xczx.upload.bean.UploadResponse;
import com.yb.xczx.upload.client.UploadClient;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.UploadObjectArgs;
import lombok.extern.slf4j.Slf4j;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/11 11:32
 * FileName: MinioUploadClient
 * Description:
 */

@Slf4j
public class MinioUploadClient implements UploadClient {

    MinioClient minioClient;

    MinioProperties minioProperties;

    public MinioUploadClient(MinioProperties minioProperties,MinioClient minioClient){

        this.minioProperties=minioProperties;
        this.minioClient=minioClient;
    }

    @Override
    public UploadResponse upload(UploadRequest uploadRequest) {

        try {
            ObjectWriteResponse response = minioClient.uploadObject(
                    UploadObjectArgs.builder()
                            .bucket(uploadRequest.getBucket())
                            .object(uploadRequest.getRemoteFilepath())
                            .contentType(uploadRequest.getContentType())
                            .filename(uploadRequest.getLocalFilepath())
                            .build());
            return new UploadResponse().setSuccess(true);
        } catch (Exception e) {
            log.info("upload error:{}",e);
        }
        return new UploadResponse().setSuccess(false);
    }
}
